package com.example.controller;

import com.example.dto.PageResult;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.logging.Logger;

@RestController
@RequestMapping("/users")
public class UserController {
    private static final Logger logger = Logger.getLogger(UserController.class.getName());

    @Autowired
    private UserService userService;
    
    /**
     * 游标分页查询接口
     * @param lastId 上一页最后一个记录的ID，第一页不传或传0
     * @param pageSize 每页大小，默认10条，最大1000条
     * @return 分页结果
     */
    @GetMapping("/cursor-page")
    public PageResult<User> cursorPage(
            @RequestParam(required = false, defaultValue = "0") Long lastId,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        long startTime = System.currentTimeMillis();

        PageResult<User> pageByCursor = userService.findPageByCursor(lastId, pageSize);
        long endTime = System.currentTimeMillis();
        logger.info("游标分页查询耗时：" + (endTime - startTime) + "ms");
        return pageByCursor;
    }
}